﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Web.fanliAdmin.Info
{
    public partial class fanli_helps : System.Web.UI.Page
    {
        #region DAL
        fanli.DAL.fanli_helps dal = new fanli.DAL.fanli_helps();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.AssertLogin(this);
            if (!IsPostBack)
            {
                BindCata();
                GridBind();
            }
        }
        private void BindCata()
        {
            ddlcata.DataSource = (new fanli.DAL.fanli_helpcata()).GetModelList(" 1=1 order by sortID ");
            ddlcata.DataBind();
            ddlcata.Items.Insert(0, new ListItem("全部", "0"));
        }
        private void GridBind()
        {
            StringBuilder sql = new StringBuilder();
            List<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter param = null;
            sql.Append("select help.*,cata.cataName from fanli_helps as help,fanli_helpcata as cata where help.cata=cata.id ");

            if (ddlcata.SelectedValue!="0")
            {
                sql.Append(" and cata.id = @cataid");
                param = new SqlParameter("@cataid", ddlcata.SelectedValue);
                paramList.Add(param);
            }
            if (txtQryId.Text != "")
            {
                sql.Append(" and help.id = @id");
                param = new SqlParameter("@id", txtQryId.Text);
                paramList.Add(param);
            }
            if (txtQryTitle.Text != "")
            {
                sql.Append(" and help.title like '%' + @title + '%'");
                param = new SqlParameter("@cataname", txtQryTitle.Text);
                paramList.Add(param);
            }
            if (txtStart.Text!="" && txtEnd.Text!="")
            {
                sql.Append(" and datediff(day,convert(datetime,@startTime) ,help.pubDate)>=0 and datediff(day,convert(datetime,@endTime) ,help.pubDate)<=0");
                param = new SqlParameter("@startTime", txtStart.Text);
                paramList.Add(param);
                param = new SqlParameter("@endTime", txtEnd.Text);
                paramList.Add(param);
            }
            if (txtQrySource.Text!="")
            {
                sql.Append(" and help.source like '%' + @source + '%'");
                param = new SqlParameter("@source", txtQrySource.Text);
                paramList.Add(param);                
            }
            sql.Append(" order by help.sortID, help.pubDate desc ");

            DataSet ds = dal.GetDatasetBySql(sql.ToString(), paramList);
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = AspNetPager1.PageSize;
            this.GridView1.DataSource = pds;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#E9F5FC'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            GridBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            if (dal.Delete(id) > 0)
            {
                GridBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            GridBind();
        }
    }
}